﻿Imports OMANE.Common.UI

Public Class ViewModelNewPMTWizardPageParamGen
    Inherits ViewModelNewPMTWizardPageBase

#Region "Déclaration"
    Private _ViewModelRacineInfocentre As ViewModelDirectoryPMT
    Private _ViewModelRacineParametres As ViewModelDirectoryPMT
    Private _DisplayName As String
#End Region

#Region "Constructeurs"
    Public Sub New(objPMT As IPMT)
        MyBase.New(objPMT)
        _ViewModelRacineInfocentre = New ViewModelDirectoryPMT(PMT.ParametresGeneraux.RacineInfocentre, "Racine de l'infocentre")
        _ViewModelRacineParametres = New ViewModelDirectoryPMT(PMT.ParametresGeneraux.RacineParametres, "Racine des paramètres")
        AddHandler _ViewModelRacineInfocentre.DirectoryExistsChanged, AddressOf OnDirectoryExistsChanged
        AddHandler _ViewModelRacineParametres.DirectoryExistsChanged, AddressOf OnDirectoryExistsChanged
        _DisplayName = "Définition"
    End Sub

    'Pour le mode design
    Public Sub New()
        MyBase.New(New PMT(New PMTParametresGeneraux() With {.AnneeRealisation = New Nullable(Of Integer)(2015), .MoisRealisation = New Nullable(Of Integer)(7)}, New PMTTablesflux))
        _ViewModelRacineInfocentre = New ViewModelDirectoryPMT(PMT.ParametresGeneraux.RacineInfocentre, "Racine de l'infocentre")
        _ViewModelRacineParametres = New ViewModelDirectoryPMT(PMT.ParametresGeneraux.RacineParametres, "Racine des paramètres")
        AddHandler _ViewModelRacineInfocentre.DirectoryExistsChanged, AddressOf OnDirectoryExistsChanged
        AddHandler _ViewModelRacineParametres.DirectoryExistsChanged, AddressOf OnDirectoryExistsChanged
        _DisplayName = "Définition"
    End Sub
#End Region

#Region "Propriétés"

    Public ReadOnly Property ListeAnneeRealisation As List(Of Integer)
        Get
            Return New List(Of Integer)({2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020})
        End Get
    End Property
    Public Property CurrentAnneeRealisation As Integer
        Get
            Return PMT.ParametresGeneraux.AnneeRealisation.GetValueOrDefault
        End Get
        Set(value As Integer)
            If (PMT.ParametresGeneraux.AnneeRealisation <> value Or PMT.ParametresGeneraux.AnneeRealisation Is Nothing) Then
                PMT.ParametresGeneraux.AnneeRealisation = New Nullable(Of Integer)(value)
                'Mettre à jour les noms des paramètres RacineInfocentre et RacineParametres
                MAJRacines()
            End If
        End Set
    End Property

    Public ReadOnly Property ListeMoisRealisation As List(Of String)
        Get
            Return New List(Of String)({"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"})
        End Get
    End Property
    Public Property CurrentMoisRealisation As String
        Get
            Return AnneeMoisConverter.MoisToString(PMT.ParametresGeneraux.MoisRealisation.GetValueOrDefault)
        End Get
        Set(value As String)
            Dim NewValue As Integer = AnneeMoisConverter.StringToMois(value)
            If (PMT.ParametresGeneraux.MoisRealisation Is Nothing Or PMT.ParametresGeneraux.MoisRealisation <> NewValue) Then
                PMT.ParametresGeneraux.MoisRealisation = New Nullable(Of Integer)(NewValue)
                'Mettre à jour les noms des paramètres RacineInfocentre et RacineParametres
                MAJRacines()
            End If
        End Set
    End Property

    Public ReadOnly Property IsAnneeMoisRealisationValide As Boolean
        Get
            Return (PMT.ParametresGeneraux.AnneeRealisation.HasValue And PMT.ParametresGeneraux.MoisRealisation.HasValue)
        End Get
    End Property

    Public ReadOnly Property BaseNomPMT As String
        Get
            Return "PMT-" & AnneeMoisConverter.AnneeMoisToString_Simple(PMT.ParametresGeneraux.AnneeRealisation, _
                                                                      PMT.ParametresGeneraux.MoisRealisation)
        End Get
    End Property
    Public Property ComplementNomPMT As String
        Get
            Return PMT.ParametresGeneraux.ComplementNom
        End Get
        Set(value As String)
            If PMT.ParametresGeneraux.ComplementNom <> value Then
                PMT.ParametresGeneraux.ComplementNom = value
            End If
        End Set
    End Property
    Public ReadOnly Property ViewModelRacineInfocentre As ViewModelDirectoryPMT
        Get
            Return _ViewModelRacineInfocentre
        End Get
    End Property
    Public ReadOnly Property ViewModelRacineParametres As ViewModelDirectoryPMT
        Get
            Return _ViewModelRacineParametres
        End Get
    End Property

    Public Overrides ReadOnly Property DisplayName As String
        Get
            Return _DisplayName
        End Get
    End Property
    Public Overrides ReadOnly Property IsValid() As Boolean
        Get
            'Return True
            Return IsAnneeMoisRealisationValide And PMT.ParametresGeneraux.RacineInfocentre.DirectoryExists And PMT.ParametresGeneraux.RacineParametres.DirectoryExists
        End Get
    End Property

#End Region

#Region "Méthodes"
    Private Sub OnDirectoryExistsChanged(Sender As Object, e As EventArgs)
        OnPropertyChanged("IsValid")
    End Sub
    Public Sub MAJRacines()
        If IsAnneeMoisRealisationValide Then
            'Le PMT gère les changements dans le modèle, il faut mettre à jour les ViewModels.
            _ViewModelRacineInfocentre.DirectoryPMT = PMT.ParametresGeneraux.RacineInfocentre
            _ViewModelRacineParametres.DirectoryPMT = PMT.ParametresGeneraux.RacineParametres
            OnPropertyChanged("BaseNomPMT")
            OnPropertyChanged("ViewModelRacineInfocentre")
            OnPropertyChanged("ViewModelRacineParametres")
            OnPropertyChanged("IsAnneeMoisRealisationValide")
            OnPropertyChanged("IsValid")
        End If
    End Sub
#End Region

End Class